package com.wangp.pdfcreator;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
//import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class PDFCreator {

	public static void main(String[] args) throws Exception {
		test();
		System.out.println("success");
	}

	public static void test() throws IOException, DocumentException {
		// pdf模板
		String fileName = "C:/Users/ad/Desktop/voucher_form.pdf";
		PdfReader reader = new PdfReader(fileName);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		/* 将要生成的目标PDF文件名称 */
		PdfStamper ps = new PdfStamper(reader, bos);
//		PdfContentByte under = ps.getUnderContent(1);

		/* 使用中文字体 */
		BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

		ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
		fontList.add(bf);

		/* 取出报表模板中的所有字段 */
		AcroFields fields = ps.getAcroFields();
		fields.setSubstitutionFonts(fontList);
		fillData(fields, data());

		/* 必须要调用这个，否则文档不会生成的 */
		ps.setFormFlattening(true);
		ps.close();

		// 生成pdf路径
		OutputStream fos = new FileOutputStream("C:/Users/ad/Desktop/test2.pdf");
		fos.write(bos.toByteArray());
		fos.flush();
		fos.close();
		bos.close();
	}

	/**
	 * 
	 * * 填充模板
	 * 
	 * *
	 */

	public static void fillData(AcroFields fields, Map<String, String> data) throws IOException, DocumentException {
		for (String key : data.keySet()) {
			String value = data.get(key);
			fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的
		}
	}

	/**
	 * 填充数据源 其中data存放的key值与pdf模板中的文本域值相对应
	 */

	public static Map<String, String> data() {
		Map<String, String> data = new HashMap<String, String>();
		data.put("start_day_cn", "李磊");
		data.put("start_day_cn", "2018-16-15");
		data.put("end_day_cn", "2018-16-15");
		data.put("hotel_type_cn", "海景房");
		data.put("room_type_cn", "大床房");
		data.put("breakfast_cn", "含早餐");
		data.put("room_num_cn", "2");
		data.put("client_name_cn", "WANG/PEI");
		data.put("confirm_num_cn", "");
		data.put("hotel_name_cn", "上海龙之梦大酒店");
		data.put("hotel_address_cn", "上海市虹口区吴淞路208号宝矿大厦");
		data.put("hotel_phone_cn", "021-123456789");
		data.put("start_hour_cn", "14:00");
		data.put("end_hour_cn", "12:00");

		data.put("start_day_en", "李磊");
		data.put("start_day_en", "2018-16-15");
		data.put("end_day_en", "2018-16-15");
		data.put("hotel_type_en", "海景房");
		data.put("room_type_en", "大床房");
		data.put("breakfast_en", "含早餐");
		data.put("room_num_en", "2");
		data.put("client_name_en", "WANG/PEI"+ System.getProperty("line.separator") +"ZHANG/SAN"+System.getProperty("line.separator")+"LI/SI"+System.getProperty("line.separator")+"WU/ZETIAN"+System.getProperty("line.separator") + "TANG/TAIZONG" + System.getProperty("line.separator")+"ZHU/YUANZHANG");
		data.put("confirm_num_en", "");
		data.put("hotel_name_en", "上海龙之梦大酒店");
		data.put("hotel_address_en", "上海市虹口区吴淞路208号宝矿大厦");
		data.put("hotel_phone_en", "021-123456789");
		data.put("start_hour_en", "14:00");
		data.put("end_hour_en", "12:00");
		return data;
	}
}
